﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Collections;
using System.Data;
using System.Text;

namespace Catering.Catering_Page.Caipin
{
    public partial class shipu_clone01 : UserIsNull
    {
        public int m_CID = 0;
        public int m_ClassID = 0;
        View_Operaction_Common voc = new View_Operaction_Common();

        protected void Page_Load(object sender, EventArgs e)
        {
            //获取参数
            m_CID = GetInt("cid", 0);
            m_ClassID = GetInt("classid", 0);
            if (m_CID == 0 || m_ClassID == 0) return;
            //初始化
            if (!IsPostBack) PageInit();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        public void PageInit()
        {
            Catering_Model.E_Recipes md_re = new Catering_Rules.T_Recipes().GetModel(m_CID);
            if (md_re == null) return;
            Catering_Model.GroupCatering md_gc = new Catering_Rules.GroupCatering().GetModel(md_re.GroupID);
            if (md_gc == null) return;

            Txt_Pic_name.Text = md_gc.GCName;           //配餐群体名称
            Txt_Pic_Te.Text = md_gc.Characteristics;    //配餐群体名称
            voc.DropDownListDataBind_Group_tedian(Dr_zuoye);//配餐群体类别

            DataTable dt = new Catering_Rules.CateringGroupInfo().GetList(" GroupID=" + md_re.GroupID).Tables[0];
            if (dt != null)
            {
                StringBuilder cgstr = new StringBuilder("");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string sexid = dt.Rows[i]["SexID"].ToString();
                    string oldareaid = dt.Rows[i]["OldAreaID"].ToString();
                    string laborgradeid = dt.Rows[i]["LaborGradeID"].ToString();
                    string number = dt.Rows[i]["Number"].ToString();
                    cgstr.Append("<tr id=\"Add_" + (i + 1) + "\">");
                    cgstr.Append("<td name=\"Add\">" + (i + 1) + "</td>");
                    cgstr.Append("<td>" + GetSexOption(sexid) + "</td>");
                    cgstr.Append("<td>" + GetAgeOption(oldareaid) + "</td>");
                    cgstr.Append("<td>" + GetLaborOption(laborgradeid) + "</td>");
                    cgstr.Append("<td> <input type=\"text\" style=\"width:50%\" id=\"Renshu\" name=\"Renshu\" value=\"" + number + "\" /></td>");
                    cgstr.Append("<td><img alt=\"\" id=\"del\" name=\"del\" onclick=\"Del_tr('" + (i + 1) + "')\" style=\"cursor:pointer;\" src=\"../../../Images/trash.gif\"/></td>");
                    cgstr.Append("</tr>");
                }
                dt.Dispose();
                this.Tbody_Pic_Group.InnerHtml = cgstr.ToString();
            }

        }

        public string GetSexOption()
        {
            return GetSexOption("");
        }

        public string GetAgeOption()
        {
            return GetAgeOption("");
        }

        public string GetLaborOption()
        {
            return GetLaborOption("");
        }

        /// <summary>
        /// 性别列表（1：男，2：女）
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string GetSexOption(string value)
        {
            StringBuilder result = new StringBuilder("");
            result.Append("<select name='sex'>");
            result.Append("<option value='1' " + (value == "1" ? "selected='selected'" : "") + ">男</option>");
            result.Append("<option value='2' " + (value == "2" ? "selected='selected'" : "") + ">女</option>");
            result.Append("</select>");
            return result.ToString();
        }

        /// <summary>
        /// 年龄列表
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string GetAgeOption(string value)
        {
            StringBuilder result = new StringBuilder("");
            DataTable dt = new Catering_Rules.OldArea().GetAllList().Tables[0];
            result.Append("<select name='Age'>");
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string id = dt.Rows[i]["id"].ToString();
                    string name = dt.Rows[i]["Area"].ToString();
                    result.Append("<option value='" + id + "' " + (value == id ? "selected='selected'" : "") + ">" + name + "</option>");
                }
                dt.Dispose();
            }
            result.Append("</select>");
            return result.ToString();
        }

        /// <summary>
        /// 劳动级别列表
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string GetLaborOption(string value)
        {
            StringBuilder result = new StringBuilder("");
            DataTable dt = new Catering_Rules.LaborGrade().GetAllList().Tables[0];
            result.Append("<select name='Labor' style='width:95%'>");
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string id = dt.Rows[i]["id"].ToString();
                    string name = dt.Rows[i]["LGName"].ToString();
                    result.Append("<option value='" + id + "' " + (value == id ? "selected='selected'" : "") + ">" + name + "</option>");
                }
                dt.Dispose();
            }
            result.Append("</select>");
            return result.ToString();
        }



        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="OptionName"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public int GetInt(string OptionName, int DefaultValue)
        {
            if (Request.QueryString[OptionName] == null) return DefaultValue;
            string tempstr = Request.QueryString[OptionName].ToString();
            Int32.TryParse(tempstr, out DefaultValue);
            return DefaultValue;
        }

        protected void Group_Next_But_Click(object sender, EventArgs e)
        {
            //保存群体配餐信息
            Catering_Model.E_Recipes md_re = new Catering_Rules.T_Recipes().GetModel(m_CID);
            Catering_Model.GroupCatering md_GC = new Catering_Rules.GroupCatering().GetModel(md_re.GroupID);
            md_GC.GCName = this.Txt_Pic_name.Text;
            md_GC.Characteristics = Txt_Pic_Te.Text;
            md_GC.temp2 = Dr_zuoye.SelectedValue;
            new Catering_Rules.GroupCatering().Update(md_GC);

            //删除配餐群体详细信息
            new Catering_Rules.CateringGroupInfo().DeleteList(" GroupID=" + md_GC.id + " ");
            //保存配餐详细信息
            foreach (DataRow row in dt_PJ.Rows)
            {
                Catering_Model.CateringGroupInfo md = new Catering_Model.CateringGroupInfo();
                md.SexID = Convert.ToInt32(row["SexID"]);
                md.OldAreaID = Convert.ToInt32(row["OldAreaID"]);
                md.LaborGradeID = Convert.ToInt32(row["LaborGradeID"]);
                md.Group_tedianID = Convert.ToInt32(row["Group_tedianID"]);
                md.Number = Convert.ToInt32(row["Number"]);
                md.GroupID = md_GC.id;
                md.id = new Catering_Rules.CateringGroupInfo().GetMaxId();
                new Catering_Rules.CateringGroupInfo().Add(md);
            }
            Response.Redirect(String.Format("shipu_clone03.aspx?cid={0}&recipesid={1}&classid={2}&IsGroupCatering=1", md_GC.id, m_CID, m_ClassID));
        }

        [WebMethod]
        public static ArrayList GetShuoMingStr(int id)
        {
            ArrayList al = new ArrayList();
            Catering_Model.Group_tedian gt = new Catering_Rules.Group_tedian().GetModel(id);
            if (gt != null) al.Add(string.IsNullOrEmpty(gt.temp1) ? "" : gt.temp1);
            return al;
        }

        [WebMethod]
        public static ArrayList GetJiaQuanPJ(string strTypes)
        {
            ArrayList al = new ArrayList();
            string[] s_Types = strTypes.TrimEnd(',').Split(',');//获取人群各个类别
            decimal[] bl_Num = new decimal[24]; //存放最后的标量值
            ArrayList No_repeatTypes = new ArrayList();//存放非重复的类别
            StringBuilder sb = new StringBuilder();//标量值查询条件
            sb.Append("1=2");

            Load_dtPJ();
            //获取不重复的配餐类别组合
            foreach (string str in s_Types)
            {
                string[] s_Num = str.Split('|');
                if (s_Num.Length < 5) continue;
                if (string.IsNullOrEmpty(s_Num[4]) || s_Num[4].Trim() == "" || s_Num[4].Trim() == "0") continue;
                string s_str = s_Num[0] + "|" + s_Num[1] + "|" + s_Num[2] + "|" + s_Num[3];//获取类别排除人数
                if (!No_repeatTypes.Contains(s_str)) No_repeatTypes.Add(s_str);
                Add_dtPJ(str);
            }

            //获取标量值条件
            foreach (string str in No_repeatTypes)
            {
                string Group_tedianId = str.Split('|')[0]; //适合人群
                string SexId = str.Split('|')[1];//性别
                string AgeId = str.Split('|')[2];//年龄段
                string LaborGradeId = str.Split('|')[3];//劳动级别
                string strwhere = "SexID=" + SexId + " and OldAreaID=" + AgeId + " and LaborGradeID=" + LaborGradeId + " and Group_tedianID=" + Group_tedianId;

                DataTable dt_IngestionIsExists = new Catering_Rules.Ingestion().GetList(strwhere).Tables[0];
                if (dt_IngestionIsExists != null && dt_IngestionIsExists.Rows.Count > 0)
                {
                    sb.Append(" or (" + strwhere + ")");
                }
                else //存在无摄入量标准的类别组合
                {
                    al.Add("NO");
                    return al;
                }
            }

            //计算标量值
            DataTable dt_Ingestion = new Catering_Rules.Ingestion().GetList(sb.ToString()).Tables[0];
            foreach (DataRow row in dt_Ingestion.Rows)
            {
                bl_Num[Convert.ToInt32(row["NutritionElementsID"]) - 1] += Convert.ToDecimal(row["Number"]);
            }

            al.Add("OK");
            //填充返回列表
            al.Add((Convert.ToDouble(bl_Num[0]) / No_repeatTypes.Count).ToString());
            for (int i = 0; i < 24; i++)
            {
                if (i == 0)
                {
                    al.Add((Convert.ToDouble(bl_Num[i]) / No_repeatTypes.Count * 4.182).ToString());
                }
                else
                {
                    al.Add((Convert.ToDouble(bl_Num[i]) / No_repeatTypes.Count).ToString());
                }
            }
            return al;
        }

        //加载群体配餐详细DT
        public static DataTable dt_PJ;
        public static void Load_dtPJ()
        {
            dt_PJ = new DataTable();
            dt_PJ.Columns.Add("SexID");
            dt_PJ.Columns.Add("OldAreaID");
            dt_PJ.Columns.Add("LaborGradeID");
            dt_PJ.Columns.Add("Group_tedianID");
            dt_PJ.Columns.Add("Number");
        }

        //填充群体配餐DT
        public static void Add_dtPJ(string strTypes)
        {
            string Group_tedianId = strTypes.Split('|')[0]; //适合人群
            string SexId = strTypes.Split('|')[1];//性别
            string AgeId = strTypes.Split('|')[2];//年龄段
            string LaborGradeId = strTypes.Split('|')[3];//劳动级别
            string Number = strTypes.Split('|')[4];//劳动级别

            DataRow row = dt_PJ.NewRow();
            row["SexID"] = SexId;
            row["OldAreaID"] = AgeId;
            row["LaborGradeID"] = LaborGradeId;
            row["Group_tedianID"] = Group_tedianId;
            row["Number"] = Number;
            dt_PJ.Rows.Add(row);
        }
    }
}